package cn.com.open.itembank.sqlprovider;

import cn.com.open.itembank.dto.TestPaperSearchDto;

import java.util.Map;

/**
 * The type Test paper read sql provider.
 */
public class TestPaperReadSqlProvider {

    /**
     * 分页获取试卷信息.
     * <p>
     * itemBankId    课程题库ID
     * isPublish     发布状态
     * isAssemble    组卷方式
     * pageNum       每页条数
     * pageSize      每页条数
     * sortOrder     排序类型
     * sortName   排序字段
     * orgid         机构ID
     * testpaperName   试卷名称
     * 试卷信息 examination paper information
     *
     * @param parameter the parameter
     * @return the test paper query
     */
    public String getTestPaperQuery(Map<String, Object> parameter) {
        TestPaperSearchDto dto = (TestPaperSearchDto) parameter.get("TestPaperSearchDto");

        String itemBankId = dto.getItemBankId();
        Integer pageSize = dto.getPageSize();
        Integer pageNumber = dto.getPageNumber();
        String testPaperName = dto.getTestpaperName();
        Integer isPublish = dto.getIsPublish();
        Integer isAssemble = dto.getIsAssemble();
        String sortName = dto.getSortName();
        String sortOrder = dto.getSortOrder();

        StringBuilder sbSql = new StringBuilder();
        sbSql.append(String.format(" SELECT TOP %d * FROM", pageSize));
        sbSql.append(String.format(" (SELECT ROW_NUMBER() OVER(ORDER BY t.%s %s) AS ROW,t.* FROM", sortName, sortOrder));
        sbSql.append(String.format(" (SELECT * FROM TestPaper WHERE State<>-1 AND CourseID = '%s'", itemBankId));
        if (isAssemble == 0 || isAssemble == 1) {
            sbSql.append(String.format(" AND Mode = %d", isAssemble));
        }
        if (isPublish == 0 || isPublish == 1) {
            sbSql.append(String.format(" AND IsPublish = %d", isPublish));
        }
        if (!testPaperName.isEmpty()) {
            sbSql.append(String.format(" AND TestPaperName LIKE '%%%s%%'", testPaperName));
        }

        sbSql.append(String.format(") t )AS DATALIST WHERE ROW > %d ", (pageNumber - 1) * pageSize));


        return sbSql.toString();
    }


    /**
     * 分页获取试卷总数
     * <p>
     * itemBankId    课程题库ID
     * isPublish     发布状态
     * isAssemble    组卷方式
     * pageNum       每页条数
     * pageSize      每页条数
     * sortOrder     排序类型
     * sortName   排序字段
     * orgid         机构ID
     * testpaperName   试卷名称
     * 试卷信息 examination paper information
     *
     * @param parameter the parameter
     * @return the test paper query
     */
    public String getTestPaperCount(Map<String, Object> parameter) {
        TestPaperSearchDto dto = (TestPaperSearchDto) parameter.get("TestPaperSearchDto");

        String itemBankId = dto.getItemBankId();
        String testPaperName = dto.getTestpaperName();
        Integer isPublish = dto.getIsPublish();
        Integer isAssemble = dto.getIsAssemble();

        StringBuilder sbSql = new StringBuilder();
        sbSql.append(String.format(" SELECT COUNT(1) FROM TestPaper WHERE State<>-1 AND CourseID = '%s'", itemBankId));
        if (isAssemble == 0 || isAssemble == 1) {
            sbSql.append(String.format(" AND Mode = %d", isAssemble));
        }
        if (isPublish == 0 || isPublish == 1) {
            sbSql.append(String.format(" AND IsPublish = %d", isPublish));
        }
        if (!testPaperName.isEmpty()) {
            sbSql.append(String.format(" AND TestPaperName LIKE '%%%s%%'", testPaperName));
        }
        return sbSql.toString();
    }
}
